<head>
    <style>
        body {
            font-family: Tahoma, Arial, Helvetica, sans-serif;
        }
        
        span {
            font-size: 10pt;
        }
        
        input {
            font-size: 10pt;
        } .statisticTitle {
            font-weight: bold;
        } .treeContainerSize {
            width: 350px;
        } .resultPanelScrollbars {
            overflow-x: scroll;
            overflow-y: scroll;
        } .tree {
            width: 1024px;
        } .icon-red {
            display: block;
            height: 16px;
            padding-left: 20px;
            background: transparent url(testudostatic/images/red_icon.jpg) 0 0px no-repeat;
        } .icon-yellow {
            display: block;
            height: 16px;
            padding-left: 20px;
            background: transparent url(testudostatic/images/yellow_icon.jpg) 0 0px no-repeat;
        } .icon-green {
            display: block;
            height: 16px;
            padding-left: 20px;
            background: transparent url(testudostatic/images/green_icon.jpg) 0 0px no-repeat;
        }
    </style>

	<!-- Individual YUI CSS files --> 
	<link rel="stylesheet" type="text/css" href="testudostatic/yui/build/treeview/assets/skins/sam/treeview.css"> 
	<!-- Individual YUI JS files --> 
	<script type="text/javascript" src="testudostatic/yui/build/yahoo/yahoo-min.js"></script> 
	<script type="text/javascript" src="testudostatic/yui/build/event/event-min.js"></script> 
	<script type="text/javascript" src="testudostatic/yui/build/dom/dom-min.js"></script> 
	<script type="text/javascript" src="testudostatic/yui/build/treeview/treeview-min.js"></script> 

</head>
<body onload="setResultPanelHeight();" onresize="setResultPanelHeight();">

    <center>
        <table width="100%">
            <col width="100%" />
            <tr>
                <td>
                    <table width="100%">
                        <col width="180px" /><col width="*" />
                        <tr>
                            <td style="vertical-align:center;">
                                <img id="resultIcon" src="testudostatic/images/green.jpg" />
                            </td>
                            <td style="vertical-align: top; horizontal-align:left;">
                                <table width="100%">
                                    <col width="150px" /><col width="*" />
                                    <tr>
                                        <td>
                                            <span class="statisticTitle">Total test count:</span>
                                        </td>
                                        <td>
                                            <span id="totalTestCountLabel" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <span class="statisticTitle">Passing test count:</span>
                                        </td>
                                        <td>
                                            <span id="passingTestCountLabel" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <span class="statisticTitle">Failing test count:</span>
                                        </td>
                                        <td>
                                            <span id="failingTestCountLabel" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <span class="statisticTitle">Ignored test count:</span>
                                        </td>
                                        <td>
                                            <span id="ignoredTestCountLabel" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <span class="statisticTitle">Total run time:</span>
                                        </td>
                                        <td>
                                            <span id="totalRunTimeLabel" />
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2">
                                <input type="button" value="Run All" onclick="runTests('?action=json');"/><input type="button" value="Expand All" onclick="expandAllNodes();"/><input type="button" value="Collapse All" onclick="collapseAllNodes();"/>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr style="vertical-align:top;">
                <td>
                    <table width="100%" border="1">
                        <tr>
                            <td class="treeContainerSize">
                                <div id="treeContainer" class="treeContainerSize resultPanelScrollbars">
                                    <div class="tree" id="resultTree">
                                    </div>
                                </div>
                            </td>
                            <td width="*" style="vertical-align:top;">
                                <div id="stackTraceContainer" class="resultPanelScrollbars">
                                    <span id="stackTraceList"/>
                                </div>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </center>
    <script>
        
        var tree;
        var root;
        var xmlhttp;
        
        function expandAllNodes(){
            if (!tree) 
                return;
            tree.expandAll();
        }
        
        function collapseAllNodes(){
            if (!tree) 
                return;
            
            tree.collapseAll();
            root.expand();
            root.focus();
        }
        
        
        function setResultPanelHeight(){
            windowHeight = (typeof window.innerHeight != 'undefined' ? window.innerHeight : document.body.offsetHeight);
            document.getElementById('treeContainer').style.height = windowHeight - 250;
            document.getElementById('stackTraceContainer').style.height = windowHeight - 250;
        }
        
        function runTests(url){
            xmlhttp = null;
            if (window.XMLHttpRequest) {// code for all new browsers
                xmlhttp = new XMLHttpRequest();
            }
            else 
                if (window.ActiveXObject) {// code for IE5 and IE6
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
            if (xmlhttp != null) {
                xmlhttp.onreadystatechange = state_Change;
                xmlhttp.open("GET", url, true);
                xmlhttp.send(null);
            }
            else {
                alert("Your browser does not support XMLHTTP.");
            }
        }
        
        function state_Change(){
            if (xmlhttp.readyState == 4) {
                if (xmlhttp.status == 200) {
                    json = xmlhttp.responseText
                    myJson = eval('(' + json + ')');
                    
                    populateTestStatistics(myJson.suite_result);
                    populateTree(myJson.suite_result);
                }
                else {
					try {
						document.getElementById("stackTraceList").innerHTML = formatLineBreaks(xmlhttp.responseText);
					} catch (e) {
						alert(e);
					}
                    alert("Problem retrieving XML data");
                }
            }
        }
        
        function populateTestStatistics(suite_result){
            if (suite_result.is_successful) 
                setResultIcon("green");
            else 
                setResultIcon("red");
            
            setLabelText("totalTestCountLabel", suite_result.total_test_count);
            setLabelText("passingTestCountLabel", suite_result.passing_test_count);
            setLabelText("failingTestCountLabel", suite_result.failing_test_count);
            setLabelText("ignoredTestCountLabel", suite_result.ignored_test_count);
            setLabelText("totalRunTimeLabel", suite_result.total_run_time);
        }
        
        function setLabelText(labelName, value){
            document.getElementById(labelName).innerHTML = value;
        }
        
        function setResultIcon(iconName){
            document.getElementById("resultIcon").src = "testudostatic/images/" + iconName + ".jpg";
        }
        
        function populateTree(suite_result){
            tree = new YAHOO.widget.TreeView("resultTree");
            root = getTreeNode("Root", null, tree.getRoot());
            for (var i = 0; i < suite_result.module_results.length; i++) {
                var module_result = suite_result.module_results[i];
                var module_result_node = getTreeNode(module_result.module_name, null, root);
                module_result_node.labelStyle = getIconForTestGroup(module_result);
                
                for (var j = 0; j < module_result.fixture_results.length; j++) {
                    var fixture_result = module_result.fixture_results[j];
                    var fixture_result_node = getTreeNode(fixture_result.fixture_name, null, module_result_node);
                    fixture_result_node.labelStyle = getIconForTestGroup(fixture_result);
                    
                    for (var k = 0; k < fixture_result.test_results.length; k++) {
                        var test_result = fixture_result.test_results[k];
                        var test_result_node = getTreeNode(test_result.method_name, test_result, fixture_result_node);
                        test_result_node.labelStyle = getIconForTestResult(test_result);
                    }
                }
            }
            root.expand();
            tree.draw();
            tree.subscribe("clickEvent", onTreeClick);
            root.focus();
            printNodeResult(root);
        }
        
        function onTreeClick(event){
            printNodeResult(event.node);
            event.node.focus();
            return false;
        }
        
        function printNodeResult(node){
            document.getElementById("stackTraceList").innerHTML = "";
            printStackTrace(node);
        }
        
        function printStackTrace(node){
            var testResult = node.data.testResult;
            if (testResult && (testResult.is_successful == false)) {
                document.getElementById("stackTraceList").innerHTML += 
					formatTestName(testResult) + "<br/>" +
                	formatStackTrace(testResult) + "<br/><hr/><br/>";
            }
            
            for (var index = 0; index < node.children.length; index++) 
                printStackTrace(node.children[index]);
        }
        
        function formatTestName(testResult){
            var color;
            if (testResult.is_ignored) 
                color = "orange";
            else 
                color = "red";
            
            return "<span style='color: " + color + "'>" +
            	testResult.method_name + "<br/>" +
	            testResult.message + "</span>";
        }
        
        function formatStackTrace(testResult){
            var replaceString = testResult.stack_trace;

			return formatLineBreaks(replaceString);
        }

		function formatLineBreaks(replaceString) {
            var indexOfMatch = replaceString.indexOf("\n");
            
            while (indexOfMatch != -1) {
                replaceString = replaceString.replace("\n", "<br/>");
                indexOfMatch = replaceString.indexOf("\n");
            }
            
            return replaceString;
		}
        
        function getIconForTestResult(testResult){
            if (testResult.is_ignored) 
                return "icon-yellow";
            else 
                if (testResult.is_successful) 
                    return "icon-green"
                else 
                    return "icon-red"
        }
        
        function getIconForTestGroup(testGroup){
            if (testGroup.is_successful) 
                return "icon-green";
            else 
                return "icon-red";
        }
        
        function getTreeNode(text, testResult, parent){
            var data = {
                "label": text,
                "testResult": testResult
            };
            return new YAHOO.widget.TextNode(data, parent, false);
        }
    </script>
</body>
